package com.MetaverseMaker.UniverseFarm.service.impl;

/**
 * @Auther: wanghuiqian
 * @Date: 2021/12/22 - 12 - 22 - 14:54
 * @Description: service.xml
 * @version: 1.0
 */



import com.MetaverseMaker.UniverseFarm.entity.*;
import com.MetaverseMaker.UniverseFarm.mapper.*;
import com.MetaverseMaker.UniverseFarm.service.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单信息管理服务层实现类
 */
@Service()
public class OrderInfoServiceImpl implements IOrderInfoService {
    // 在userService中保存一个dao的引用。用来操作数据库
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private SoilInfoMapper soilInfoMapper;
    @Autowired
    private OrderInfoVOMapper orderInfoVOMapper;
    @Autowired
    private OrderItemVOMapper orderItemVOMapper;

    @Override
    public OrderInfo queryById(int orderId) {
        return orderInfoMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public List<OrderInfo> queryAllOrder() {
        return orderInfoMapper.selectByExample(null);
    }

    /**
     * 订单的生成
     *
     * @param cart   购物车信息
     * @param userId 用户id信息
     * @return 返回订单号
     * @throws Exception
     */
    @Override//订单的生产
    public String saveOrder(CartInfoServiceImpl cart, int userId) throws Exception {
        // 创建订单对象
        OrderInfo order = new OrderInfo();
        Date dateTime = new Date();
        //将Date类型转化为String类型并存至订单对象中的结算时间
        String time;
        SimpleDateFormat formater = new SimpleDateFormat();
        formater.applyPattern("yyyy-MM-dd HH:mm");
        time = formater.format(dateTime);
        order.setOrderTime(time);
        //将BigDecimal类型转化为String类型并存至订单对象中的总金额
        cart.getTotalMoney().setScale(1, BigDecimal.ROUND_HALF_UP);
        String moneyString = String.valueOf(cart.getTotalMoney());
        order.setOrderMoney(moneyString);
        String countString = String.valueOf(cart.getTotalCount());
        order.setOrderNumber(countString);
        order.setUserId(userId);
        order.setOrderStatus("未配送");
        // 生成订单号
        char paramChar = (char) (Math.random() * 26 + 'A');
        String number = String.valueOf((int) ((Math.random() * 9 + 1) * 1000000));
        String orderName = paramChar + number;

        order.setOrderName(orderName);
        // 保存订单
        int i = orderInfoMapper.insertSelective(order);
        OrderInfoExample example=new OrderInfoExample();
        example.createCriteria().andOrderNameEqualTo(orderName);
        OrderInfo orderInfo=orderInfoMapper.selectByExample(example).get(0);
        order.setOrderId(orderInfo.getOrderId());
        System.out.println(order.getOrderId());

        // 遍历购物车中有每一个商品
        List<OrderItem> items = new ArrayList<OrderItem>();
        for (Cart cartItem : cart.getItems().values()) {
            // 生成订单项
            OrderItem orderItem = new OrderItem(0,order.getOrderId(), cartItem.getId(), cartItem.getCount(),
                    cartItem.getTotalMoney());
            System.out.println(orderItem.getOrderId());
            items.add(orderItem);
            // 修改土地
//            SoilInfo soilInfo=soilInfoMapper.selectByPrimaryKey(cartItem.getId());
//            soilInfoMapper.deleteByPrimaryKey(cartItem.getId());
//            Dishes dishes = iDishesDao.queryIdDishes(cartItem.getId());
//            dishes.setDishes_number(String.valueOf(Integer.valueOf(dishes.getDishes_number()) - Integer.valueOf(cartItem.getCount())));
//            dishes.setDishes_sales(String.valueOf(Integer.valueOf(dishes.getDishes_sales()) + Integer.valueOf(cartItem.getCount())));
//            iDishesDao.updateDishes(dishes);

        }
        // 批量插入
        for (OrderItem item : items) {
            orderItemMapper.insert(item);
        }
        // 清空购物车
        cart.clear();
        if (i > 0) {
            return order.getOrderName();
        } else
            return "-1";
    }

    /**
     * 我的订单列表
     *
     * @param userId 用户id号
     * @return 返回用户的订单信息
     * @thro ws Exception
     */
    @Override
    public List<OrderInfo> queryUserOrders(int userId) throws Exception {
        OrderInfoExample orderInfoExample=new OrderInfoExample();
        orderInfoExample.createCriteria().andUserIdEqualTo(userId);
        return orderInfoMapper.selectByExample(orderInfoExample);
    }

    /**
     * 订单详情
     *
     * @param orderId 要查询的订单号
     * @return 订单详细
     * @throws Exception
     */
    @Override
    public List<OrderItemVO> queryOrderItems(int orderId) throws Exception {
        return orderItemVOMapper.queryIDOrderItemVO(orderId);
    }

    /**
     * 商铺--查看所有订单
     *
     * @throws Exception
     */
//    @Override
//    public List<OrderInfoVO> queryShopOrders(int pageNum, int pageSize) throws Exception {
//        //参数pageNum是页码数值，pageSize是查询每页的参数，必须写在调用分页的代码的前面
//        PageHelper.startPage(pageNum, pageSize);
//        return orderInfoVOMapper.queryOrderInfoVO();
//    }

    /**
     * 商铺--通过关键字查看订单
     *
     * @throws Exception
     */
//    @Override
//    public List<OrderInfoVO> queryOrdersByKeyWord(int pageNum, int pageSize, String kw) throws Exception {
//        //参数pageNum是页码数值，pageSize是查询每页的参数，必须写在调用分页的代码的前面
//        PageHelper.startPage(pageNum, pageSize);
//        return orderInfoVOMapper.queryKWOrderInfoVO(kw);
//    }

    /**
     * 修改订单状态
     *
     * @param orderId 配送的订单号
     * @throws Exception
     */
    @Override
    public void updateOrder(int orderId, String status) throws Exception {
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);

        orderInfo.setOrderStatus(status);/*修改状态*/
        UserInfo lastUserInfo = userInfoMapper.selectByPrimaryKey(orderInfo.getUserId());
        if(status.equals("已完成")){
            Double lastInteger = lastUserInfo.getUserIntegral();
            Double addInteger = Double.valueOf(orderInfo.getOrderMoney());
//            int currentInteger = lastInteger+addInteger*10;
            Double currentInteger=lastInteger-addInteger;
            lastUserInfo.setUserIntegral(currentInteger);
            userInfoMapper.updateByPrimaryKeySelective(lastUserInfo);
//            iUserInfoDao.updateUserIntegral(String.valueOf(currentInteger),orderInfo.getUser_id());
            System.out.println(currentInteger);
//            LoggerUtil.setINFOLoggers("用户"+lastUserInfo.getUser_account()+"积分更新为："+currentInteger);
        }
        orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
    }

    /**
     * 修改状态为已配送
     *
     * @param orderId 配送的订单号
     * @throws Exception
     */
    @Override
    public void serveOrder(int orderId) throws Exception {
        OrderInfo serve = new OrderInfo();
        serve.setOrderId(orderId);
        serve.setOrderStatus("已配送");
        orderInfoMapper.updateByPrimaryKeySelective(serve);
    }


    /**
     * 修改状态为未接单
     *
     * @param orderId 未接单的订单号
     * @throws Exception
     */
    @Override
    public void noReceiveOrder(int orderId) throws Exception {
        OrderInfo noReceive = new OrderInfo();
        noReceive.setOrderId(orderId);
        noReceive.setOrderStatus("未接单");
        orderInfoMapper.updateByPrimaryKeySelective(noReceive);
//        iOrderDao.updateOrderInfo(noReceive);
    }

    /**
     * 修改状态为已接单
     *
     * @param orderId 已接单的订单号
     * @throws Exception
     */
    @Override
    public void receiveOrder(int orderId) throws Exception {
        OrderInfo receive = new OrderInfo();
        receive.setOrderId(orderId);
        receive.setOrderStatus("已接单");
        orderInfoMapper.updateByPrimaryKeySelective(receive);
//        iOrderDao.updateOrderInfo(receive);
    }

    /**
     * 修改状态为已完成
     *
     * @param orderId 已完成的订单号
     * @throws Exception
     */
    @Override
    public void completeOrder(int orderId) throws Exception {
        OrderInfo complete = new OrderInfo();
        complete.setOrderId(orderId);
        complete.setOrderStatus("已完成");
        orderInfoMapper.updateByPrimaryKeySelective(complete);
//        iOrderDao.updateOrderInfo(complete);
    }

    /**
     * 修改状态为已取消
     *
     * @param orderId 已取消的订单号
     * @throws Exception
     */
    @Override
    public void noCompleteOrder(int orderId) throws Exception {
        OrderInfo noComplete = new OrderInfo();
        noComplete.setOrderId(orderId);
        noComplete.setOrderStatus("已取消");
        orderInfoMapper.updateByPrimaryKeySelective(noComplete);
//        iOrderDao.updateOrderInfo(noComplete);
    }

    /**
     * 订单报表信息管理
     *
     * @return
     * @throws Exception
     */
//    @Override
//    public XSSFWorkbook ordersExcelExport() throws Exception {
//        List<OrderInfoVO> orderInfoVOList = queryAllOrders();
//        List<OrderItemVO> orderItemVOList = queryAllOrderItems();
//        return ExcelExportUtil.getXSSFWorkbook(orderInfoVOList, orderItemVOList);
//
//    }

    /**
     * 查询订单信息视图里的所有信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderInfoVO> queryAllOrders() throws Exception {
        return orderInfoVOMapper.queryOrderInfoVO();
//        return iOrderInfoVODao.queryOrderInfoVO();
    }


    /**
     * 查询订单详情里的所有信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderItemVO> queryAllOrderItems() throws Exception {
        return orderItemVOMapper.queryAllOrderItemVO();
//        return iOrderItemVODao.queryAllOrderItemVO();
    }


}
